﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.IO;

public class Global : System.Web.HttpApplication
{

    protected void Application_Start(object sender, EventArgs e)
    {
        Application.Lock();
        StreamReader streamReader = null;
        if (File.Exists(Server.MapPath("COUNTER.txt")))
        {
            streamReader = new StreamReader(Server.MapPath("COUNTER.txt"));
        }

        if (streamReader != null)
        {
            string strcouter = streamReader.ReadLine();
            int couter = int.Parse(strcouter);
            streamReader.Close();
            Application.UnLock();

            Application["Total"] = couter;
        }
        Application["Online"] = 0;
    }

    protected void Session_Start(object sender, EventArgs e)
    {
        Session["UserName"] = null;

        //---------------Count member online and total member
        if (Application["Online"] != null)
        {
            Application["Online"] = (int)Application["Online"] + 1;
        }
        else
        {
            Application["Online"] = 0;
        }
        if (Application["Total"] != null)
        {
            Application["Total"] = (int)Application["Total"] + 1;
            StreamWriter streamWriter = null;
            if (File.Exists(Server.MapPath("COUNTER.txt")))
                streamWriter = new StreamWriter(Server.MapPath("COUNTER.txt"));
            if (streamWriter != null)
            {
                streamWriter.Write((Application["Total"].ToString()));
                streamWriter.Close();
            }
        }
        else
        {
            Application["Total"] = 0;
        }
    }

    protected void Application_BeginRequest(object sender, EventArgs e)
    {

    }

    protected void Application_AuthenticateRequest(object sender, EventArgs e)
    {

    }

    protected void Application_Error(object sender, EventArgs e)
    {

    }

    protected void Session_End(object sender, EventArgs e)
    {
        Session["UserName"] = null;
        Application["Online"] = (int)Application["Online"] - 1;
    }

    protected void Application_End(object sender, EventArgs e)
    {

    }
}
